package org.cosmact.designer.vxml.gefeditor.editpart.policy;

import org.cosmact.designer.vxml.gefeditor.editpart.DialogEditPart;
import org.cosmact.designer.vxml.gefeditor.editpart.command.CreateDialogCommand;
import org.cosmact.designer.vxml.gefeditor.editpart.command.MoveDialogCommand;
import org.cosmact.vxml.model.Dialog;
import org.cosmact.vxml.model.Document;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gef.requests.CreateRequest;

public class DocumentXYLayoutEditPolicy extends
		org.eclipse.gef.editpolicies.XYLayoutEditPolicy {

	public DocumentXYLayoutEditPolicy(XYLayout layout) {
		super();
		setXyLayout(layout);
	}

	@Override
	protected Command createChangeConstraintCommand(EditPart child,
			Object constraint) {
		// return new MoveDialogCommand(child, (Rectangle) constraint);
		return null;
	}

	@Override
	protected Command createChangeConstraintCommand(
			ChangeBoundsRequest request, EditPart child, Object constraint) {
		if (child instanceof DialogEditPart && constraint instanceof Rectangle) {
			// return a command that can move and/or resize a Shape
			return new MoveDialogCommand(request, child, (Rectangle) constraint);
		}
		return super.createChangeConstraintCommand(request, child, constraint);
	}

	@Override
	protected Command getCreateCommand(CreateRequest request) {
		Object childClass = request.getNewObjectType();
		if (childClass == Dialog.class) {
			// return a command that can add a Dialog to a Document
			return new CreateDialogCommand((Dialog) request
					.getNewObject(), (Document) getHost().getModel(),
					(Rectangle) getConstraintFor(request));
		}
		return null;
	}

}
